/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import java.util.ArrayList;
import java.util.List;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.common.client.model.AttributeModel;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.TextBox;

/**
 *  
 */
public class ProductOptionsComponent extends EditTableComponent
{	
	protected List<AttributeModel> attributeModels;
	protected AttributeModel emptyAttributeModel;

	/**
	 * Constructor
	 */
	public ProductOptionsComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);
		emptyAttributeModel = new AttributeModel();
		attributeModels = new ArrayList<AttributeModel>();
		paginate = false;
		tableType = "product options";
	}

	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
		super.buildMetaModel(jsonObject);
	}

	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		attributeModels.clear();
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		AttributeModel model = new AttributeModel(jsonObject);
		attributeModels.add(model);
	}

	public void buildFromModel() throws Exception
	{
		numberOfTableEntries = attributeModels.size();
		if (edit)
			numberOfTableColumns = 3;
		else 
			numberOfTableColumns = 2;
		extraRows = 2;
		super.buildFromModel();
	}

	public void displayHeader(Grid grid) {
		grid.setText(0, 0, "Id");
		grid.setText(0, 1, "Name");
		if (edit) grid.setText(0, 2, "Mark for Deletion");
	}

	public void displayRow(Grid grid, int row, int entryIndex) {
		displayRow(grid, row, attributeModels.get(entryIndex));
	}

	public void displayEmptyRow(Grid grid, int row) {
		displayRow(grid, row, emptyAttributeModel);
	}

	public void displayRow(Grid grid, int row, AttributeModel model) {
		if (edit) {
			
			TextBox idTextBox = new TextBox();
			idTextBox.setWidth("80px");
			idTextBox.setText(String.valueOf(model.getId()));
			elements.put("productOptionId-" + row, idTextBox);
			if (model.getId() > 0) {
				grid.setText(row, 0, String.valueOf(model.getId()));
			}
			else {
				grid.setWidget(row, 0, idTextBox);
			}
			
			TextBox nameTextBox = new TextBox();
			nameTextBox.setWidth("300px");
			nameTextBox.setText(model.getName());
			elements.put("productOptionName-" + row, nameTextBox);
			grid.setWidget(row, 1, nameTextBox);

			if (model.getId() > 0) {
				CheckBox deleteCheckBox = new CheckBox();
				elements.put("productOptionDelete-" + row, deleteCheckBox);
				grid.setWidget(row, 2, deleteCheckBox);
			}
		}
		else {
			grid.setText(row, 0, String.valueOf(model.getId()));
			grid.setText(row, 1, model.getName());
		}
	}

	public List<AttributeModel> getOptionModels() {
		return attributeModels;
	}
}
